package tosp.search;

import org.apache.log4j.Logger;

import tosp.HistoryMachine;
import tosp.JobSequence;
import tosp.job.Job;

public class RandomNeighborSearch extends LocalSearchAdapter {

	static Logger log = Logger.getLogger(RandomNeighborSearch.class);
	
	public RandomNeighborSearch(HistoryMachine m) {
		super(m);
	}

	@Override
	public JobSequence[] getNeighbors(JobSequence j) {
		JobSequence[] ret=new JobSequence[j.jobs.length*10];
		Job[] jobs=j.getJobsClone();
		Job t;
		
		for(int i=0;i<ret.length;i++) {
			for (int r=0;r<Math.max(10, jobs.length/3);r++) {
				int from=(int)(Math.random()*jobs.length);
				int to=(int)(Math.random()*jobs.length);
				t=jobs[to];
				jobs[to]=jobs[from];
				jobs[from]=t;
			}
			JobSequence newjs = new JobSequence(jobs,true);
			log.trace("Generated new Neighbor: "+newjs.getSeqAsString());
			ret[i]=newjs;
		}
		log.debug("Generated a Total of "+ ret.length +" Neighbors");
		return ret;
	}

}
